package com.timer.taskRule;

import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Lock;

/***
 * 期望： 只存在一个线程 传入true时 线程已开启则return 没开启则start
 *                    传入false 已开启则关闭 没开启return
 *                    问题： 传入false时 没停止线程
 */
public class ScanThread implements Runnable{

    private static ScanThread scanThread;

    private Thread thread;

    //是否停止线程
    private boolean cancelled;


    private int time = 10;

    private static ExecutorService executorService = Executors.newFixedThreadPool (2);

    //单例创建
/*    public static ScanThread getInstance(){
        if(scanThread==null){
            synchronized(ScanThread.class){
                if(scanThread==null) {
                    scanThread = new ScanThread();
                }
            }
        }
        return scanThread;
    }*/

    public ScanThread(int time) {
        this.time = time;
    }

    //五秒扫描一次文件
    @Override
    public void run() {
        while (this.cancelled){
            synchronized (this){
                if(this.cancelled){
                    try {
                        System.out.println(Thread.currentThread().getName()+"扫描文件结束" );
                        Thread.sleep(time * 1000);
                        System.out.println("睡眠结束");
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        System.out.println("结束");
    }

    public void setTime(int time){
        this.time = time;
    }

    public  void scan(Boolean state){
        this.cancelled = state;
        if(this.cancelled){
            if(thread == null ||
                    (thread != null && !thread.isAlive())){
                thread = new Thread(this,"ScanFileThread");
            }else if(thread.isAlive()){
                return;
            }
            thread.start();
        }
    }

    public  void scan2(Boolean state){
        this.cancelled = state;
        if(this.cancelled){
            //new Thread(this,"scan").start();
            executorService.execute(this);
        }
    }

    public static void open(boolean key){
        if(key){
            if(scanThread == null){
                scanThread = new ScanThread(10);
                scanThread.scan2(key);
            }else{
                scanThread.scan2(!key);
                scanThread = null;
                open(key);
            }
        }else{
            scanThread.scan2(key);
            scanThread = null;
        }
    }

    public static void main(String[] args) {
        try {
            boolean key = true;
            System.out.println("第一次开启");
            open(key);
            Thread.sleep(2000);

            key = false;
            System.out.println("第一次关闭");
            open(key);

            Thread.sleep(2000);

            key = true;
            System.out.println("第二次开启");
            open(key);

            Thread.sleep(4000);

            key = false;
            System.out.println("第二次关闭");
            open(key);

            Thread.sleep(2000);

            key = true;
            System.out.println("第三次开启");
            open(key);


        } catch (InterruptedException e) {
            e.printStackTrace();
        }
     /*   ScanThread scanThread = ScanThread.getInstance();
        scanThread.scan(true);

        try {
            Thread.sleep(10000);
            System.out.println("关闭线程");
            ScanThread scanThread2 = ScanThread.getInstance();
            scanThread2.scan(false);

            Thread.sleep(300);
            System.out.println("第二次开启线程");
            ScanThread scanThread3 = ScanThread.getInstance();
            scanThread3.scan(true);

            scanThread3.setTime(1);

        } catch (InterruptedException e) {
            e.printStackTrace();
        }*/
    }


}
